<!-- 编辑弹窗 -->
<template>
  <el-dialog
    :destroy-on-close="true"
    :lock-scroll="false"
    :title="isUpdate?'编辑联系人':'新建联系人'"
    :visible="visible"
    custom-class="ele-dialog-form"
    width="600px"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      label-width="92px"
      @keyup.enter.native="save"
      @submit.native.prevent>
      <el-form-item label="姓名:">
        <el-input v-model="form.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="性别:" prop="gender">
        <el-select v-model="form.gender" class="w-full" placeholder="请选择">
          <el-option label="男" value="male" />
          <el-option label="女" value="female" />
          <el-option label="保密" value="secret" />
        </el-select>
      </el-form-item>

      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="客户单位" prop="phone">
        <el-autocomplete
          v-model="customer_name"
          :fetch-suggestions="querySearchAsync"
          class="w-full"
          placeholder="搜索客户"
          value-key="customer_name"
          @select="handleSelect"
        />
      </el-form-item>
      <el-form-item label="办公电话" prop="office_phone">
        <el-input v-model="form.office_phone" placeholder="请输入办公电话" />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入电子邮箱" />
      </el-form-item>
      <el-form-item label="出生日期" prop="email">
        <el-date-picker
          v-model="form.birthday"
          class="c-picker"
          placeholder="选择出生日期"
          type="date"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <el-form-item label="职务" prop="duty">
        <el-input v-model="form.duty" placeholder="请输入职务" />
      </el-form-item>
      <el-form-item label="项目角色" prop="project_role">
        <el-input v-model="form.project_role" placeholder="请输入项目角色" />
      </el-form-item>
      <el-form-item label="所属区域:" >
        <el-cascader
          clearable
          v-model="city"
          class="ele-fluid"
          placeholder="请选择省市区"
          :options="regions.cityData"
          popper-class="ele-pop-wrap-higher"/>
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="form.address" placeholder="请输入详细地址" />
      </el-form-item>
      <el-form-item label="默认联系人" prop="project_role">
        <el-checkbox v-model="form.is_default" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        :loading="loading"
        type="primary"
        @click="save">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

import regions from 'ele-admin/packages/regions';

export default {
  name: 'AddConcat',
  components: {},
  props: {
      visible:Boolean
  },
  data() {
    return {
      regions: regions,
      // 选中的省市区
      city:[],
      // 选中的省市
      provinceCity: [],
      // 选中的省
      province: [],
      // 表单数据
      form: {},
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 回顯客戶單位名稱
      customer_name: ""
    };
  },
  watch: {

  },

  mounted() {

  },
  methods: {
    //所属单位远程
    querySearchAsync(query, cb) {
      this.$http.get("customer_users", {
        params: {
          keyword: query,
          page: 1,
          limit: 10
        }
      }).then((res) => {
        cb(res.data.data);
      })
    },
    handleSelect(item) {
      this.form.customer_id = item.customer_id;
    },
    //更新状态
    updateVisible(value){
      this.form={}
      this.$emit('update:visible', value);
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form = Object.assign({}, this.form, {
            province_id :this.city[0] ,
            city_id :this.city[1],
            county_id:this.city[2]
          });
          this.loading = true;
          this.$http.post('customer_users', Object.assign({}, this.form, {
            pid: this.form.pid || 0
          })).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
  }
}
</script>

<style>
.el-select {
  width: 100%;
}

.el-autocomplete {
  width: 100%;
}

.el-input {
  width: 100% !important;
}
</style>
